#!/bin/bash

. ./config.cfg
set -x
MARATHON_URL="$MARATHON_URL"
TSUNG_SLAVE_MARATHON_ID=`./get_app_name.sh`
MAXUSER="1000"
CPU="1"
XML_PATH="$BASE_DIR/config/tsung"
XML_FILE="${XML_PATH}/tsung.xml"

HOST_NAME(){
if [ -n "${MARATHON_URL}" ];then
        HOSTS_FILE=$(curl -s ${MARATHON_URL}/v2/apps/${TSUNG_SLAVE_MARATHON_ID} | awk -F 'lastTaskFailure' '{print $1}' | grep -Po '"host":"\K.*?(?=")' |sort -n | awk '{ print $1" TsungSalve"NR}'|awk '{print $1","$2}')
else
        echo "MARATHON_URL is null!!"
        exit 1
fi
}

SLAVE(){
HOST_NAME
for i in  $HOSTS_FILE
do
        IP=$(echo $i | awk -F ',' '{print $1}')
        HOSTNAME=$(echo $i | awk -F ',' '{print $2}')
        echo "    <client host=\"$HOSTNAME\"  maxusers=\"$MAXUSER\" cpu=\"$CPU\" >"
        echo "          <ip value=\"$IP\"></ip>"
        echo "    </client>"
done
}

MONITOR(){
HOST_NAME
for i in  $HOSTS_FILE
do
        HOSTNAME=$(echo $i | awk -F ',' '{print $2}')
        echo "    <monitor host=\"$HOSTNAME\" type=\"erlang\"></monitor>"
done
}
FRIST(){
#cat << EOF > ${XML_FILE}
cat << EOF > ${XML_FILE}
<?xml version="1.0"?>
<!DOCTYPE tsung SYSTEM "/usr/local/tsung/share/tsung/tsung-1.0.dtd">
<tsung loglevel="warning" version="1.0">

  <!-- Client side setup -->
  <clients>
EOF
}

SECOND(){
cat << EOF >> ${XML_FILE}
  </clients>

 <!-- Server side setup -->
<servers>
  <server host="192.168.1.215" port="8010" type="tcp"></server>
</servers>

  <!-- to start os monitoring (cpu, network, memory). Use an erlang
  agent on the remote machine or SNMP. erlang is the default -->

  <monitoring>
EOF
}

THIRD(){
cat << EOF >> ${XML_FILE}
 </monitoring>

<load>
  <!-- several arrival phases can be set: for each phase, you can set
  the mean inter-arrival time between new clients and the phase
  duration -->
   <arrivalphase phase="1" duration="10" unit="minute">
     <users maxnumber="5000" arrivalrate="300" unit="second"></users>
   </arrivalphase>
</load>

  <options>
   <option type="ts_http" name="user_agent">
    <user_agent probability="80">Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20050513 Galeon/1.3.21</user_agent>
    <user_agent probability="20">Mozilla/5.0 (Windows; U; Windows NT 5.2; fr-FR; rv:1.7.8) Gecko/20050511 Firefox/1.0.4</user_agent>
   </option>
  </options>

  <!-- start a session for a http user. the probability is the
  frequency of this type os session. The sum of all session's
  probabilities must be 100 -->

 <sessions>
  <session name="dataman-test" probability="100" type="ts_http">
    <!-- full url with server name, this overrides the "server" config value -->
    <for from="1" to="6000" var="i">
        <request>
                <http url="/time.php" method="GET" version="1.1">
                        <http_header name="Cache-Control" value="no-cache"/>
                </http>
        </request>
        <thinktime value="1" random="true"></thinktime>
    </for>
</session>
 </sessions>
</tsung>
EOF
}

RUN(){
SLAVE=`SLAVE`
MONITOR=`MONITOR`

if [ -f  "${XML_FILE}" ];then
        rm -Rf ${XML_FILE}
fi
FRIST
        cat << EOF >> ${XML_FILE}
$SLAVE
EOF
SECOND
        cat << EOF >> ${XML_FILE}
$MONITOR
EOF
THIRD
}
RUN   
